﻿//.....................................公共业务.........................
//公共变量
var key = '关键字';
var page = 1;
var size = 10;

//初始化
$(function () {
    //bindEvent();
});

//绑定事件
function bindEvent() {
    $('#key').placeHold(key);
    $("#getBtn").click(function (event) {
        bindData(1);
    });
    //监听回车，实现搜索
    //QiBu.Api.Common.bindEnter('key', 'getBtn');
}

//发送异步请求
function sendAjax(url, data, before, success) {
    $.ajax({
        url: url,
        type: 'psot',
        dataType: 'json',
        data: data,
        cache: false,
        timeout: 30000,
        beforeSend: before,
        success: function (json) {
            success(json)
        },
        error: function (xhr, status, error) {
            qibu.msgbox.hide();
            dlg.Com("dlg-name", "出错啦！", xhr.responseText, 450, function () {
                dlg.cls("dlg-name");
            });
        }
    });
}

//获取列表
function bindData(option) {
    var _key = $.trim($("#key").val());
    _key = (_key == key ? '' : _key);
    page = option.index || 1;
    size = $('#selSize').val() || 10;
    sendAjax(option.url, option.data, option.before, option.success);
}

//删除记录
function deleteRecord(id, title, url, success) {
    dlg.Confirm('提示', '确定删除记录【' + title + '】？', function () {
        sendAjax(url, { id: id }, function () { }, function (data) {
            success(data);
        });
    });
}
//jqwidgets公用
var source;
var dataAdapter;
//数据源配置
function getSource(id, cols, url) {
    source = {
        dataType: "json",
        dataFields: cols,
        hierarchy: { root: 'children' },
        id: id,
        url: url,
        pagenum: 0,
        pagesize: 50
    };
}
//数据适配器
function getAdapter() {
    dataAdapter = new $.jqx.dataAdapter(source,
              {
                  formatData: function (data) {
                      data.$skip = data.pagenum * data.pagesize;
                      data.$top = data.pagesize;
                      data.$inlinecount = "allpages";
                      sortdatafield: data.sortdatafield
                      sortorder: data.sortorder
                      return data;
                  },
                  downloadComplete: function (data, status, xhr) {
                      if (!source.totalRecords) {
                          source.totalrecords = parseInt(data.TotalRows);
                      }
                  },
                  loadError: function (xhr, status, error) {
                      dlg.Com("dlg-name", "出错啦！", xhr.responseText, 450, function () {
                          dlg.cls("dlg-name");
                      });
                  }
              }
          );
}
//渲染Grid组件
function getGridData(bars, ready, cols) {
    $("#datagrid").jqxTreeGrid(
        {
            width: '99.8%',
            height: '99.8%',
            //theme: 'arctic',
            source: dataAdapter,
            showToolbar: true,//是否显示工具栏
            sortable: true,//支持排序
            columnsResize: true,
            pageable: true,//是否分页
            pagerMode: 'advanced',//分页样式
            pageSize: 50,//每页多少条记录
            pageSizeOptions: ['20', '50', '100'],
            serverProcessing: true,
            renderToolbar: bars,
            ready: ready,
            columns: cols
        });
}